package com.wffz.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wffz.comm.api.ResponseResult;
import com.wffz.comm.utils.CodeUtils;
import com.wffz.comm.utils.DateUtils;
import com.wffz.comm.utils.RestTemplateUtil;
import com.wffz.po.ClientUser;
import com.wffz.po.MobileVerificationCode;
import com.wffz.service.MobileVerificationCodeService;
import com.wffz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RequestMapping("/login")
@RestController
@Slf4j
public class LoginRestController {
    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Autowired
    private MobileVerificationCodeService mobileVerificationCodeService;

    @Autowired
    private UserService userService;

    /**
     * @Description: [发送验证码手机号]
     * @Title: sendPhoneCode
     * @Author: SUNLEI
     * @Date: 2022-12-23
     * @Param: mobileNumber
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @PostMapping("/sendPhoneCode")
    public ResponseResult sendPhoneCode(String mobileNumber, Integer type) {
        log.info("发送验证码手机号{}", mobileNumber);
        String mobileCode = CodeUtils.getStringRandom(4);
        log.info("发送的验证码{}", mobileCode);
//        String postUrl = "http://openapi.xunmiao.net/api/intl-sms/send";
        String postUrl = "https://api.4321.sh/inter/send";
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("account", "lGpzspGnHhZpdQJR");
//        jsonObject.put("password", "hG5dTZNZVy5LMbO78NrKccQajl6TrCRk");
//        jsonObject.put("phone", mobileNumber);
//        jsonObject.put("msg", "[STPLOD]Your verification code is " + mobileCode);
//        jsonObject.put("sendtime", DateUtils.getTime());

        jsonObject.put("apikey","I97791b283");
        jsonObject.put("secret","977918aaa31a0696");
        jsonObject.put("mobile",mobileNumber);
        jsonObject.put("content",mobileCode);
        jsonObject.put("sendtime", DateUtils.getTime());
        jsonObject.put("template_id", 106547);
        String result = restTemplateUtil.httpPost(postUrl, jsonObject);
        log.info("发送验证码返回值：{}", result);
        JSONObject resultObj = JSONObject.parseObject(result);
        if (resultObj.getString("msg").equals("OK")) {
            log.info("新增验证码：");
            MobileVerificationCode mobileVerificationCode = new MobileVerificationCode();
            mobileVerificationCode.setVerificationCode(mobileCode);
            mobileVerificationCode.setMobileNumber(mobileNumber);
            mobileVerificationCode.setCodeType(type);
            mobileVerificationCode.setCodeStatus(0);
            mobileVerificationCode.setCreateTime(new Date());
            mobileVerificationCodeService.saveMobileVerificationCode(mobileVerificationCode);
        }
        return ResponseResult.success(resultObj);
    }

    /**
     * @Description: []
     * @Title: isEfficacious
     * @Author: SUNLEI
     * @Date: 2023-02-01
     * @Param: mobileNumber
     * @Param: phoneCode
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @GetMapping("/isEfficacious")
    public ResponseResult isEfficacious(String mobileNumber, String phoneCode) {
        boolean flag = mobileVerificationCodeService.isEfficacious(mobileNumber, phoneCode);
        return ResponseResult.success(flag);
    }

    /**
     * @Description: []
     * @Title: resetPwd
     * @Author: SUNLEI
     * @Date: 2023-02-01
     * @Param:
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @PostMapping("/resetPwd")
    public ResponseResult resetPwd(String mobileNumber, String pwd) {
        ClientUser clientUser = userService.findUserByUserName(mobileNumber);
        clientUser.setLoginPassword(pwd);
        boolean flag = userService.updateClientUser(clientUser);
        return ResponseResult.success(flag);
    }

}
